package com.cz.basetool.ui_work.util.time;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.cz.basetool.ui_work.interfaces.IDestroy;
import com.cz.basetool.ui_work.util.log.AndroidConsoleLogPrinter;

/**
 * Created by 陈章 on 2017/4/6 0006.
 * func:
 * 计时器 每0.1秒计一次
 */

public class TimeCounter implements IDestroy {
    private static final String TAG = "TimeCounter";
    private final int MSG_TYPE_RECORD_START = 0;     //开始记录学习时间
    private final int MSG_TYPE_RECORD_PAUSE = 1;     //开始记录学习时间
    private final int MSG_TYPE_RECORD_RESET = 2;     //重置学习时间
    private  long delayTime = 1000;
    private int passedCount;  //次数
    private TimeCallBack timeCallBack;
    private boolean isDestroyed = false;

    private final Runnable recordTimeTask = new Runnable() {
        @Override
        public void run() {
            passedCount++;
            if (timeCallBack != null) {
                timeCallBack.onTime(passedCount, delayTime);
            }
            if (!isDestroyed) {
                recordTimeHandler.sendEmptyMessage(MSG_TYPE_RECORD_START);
            }
        }
    };

    private final Handler recordTimeHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
//            AndroidConsoleLogPrinter.e(TAG, "handleMessage: " + msg.what);
            switch (msg.what) {
                case MSG_TYPE_RECORD_START:
                    recordTimeHandler.postDelayed(recordTimeTask, delayTime);
                    break;
                case MSG_TYPE_RECORD_PAUSE:
                    recordTimeHandler.removeMessages(0);
                    break;
                case MSG_TYPE_RECORD_RESET:
                    passedCount = 0;
                    recordTimeHandler.removeCallbacks(recordTimeTask);
                    recordTimeHandler.removeMessages(0);
                    break;
            }
        }
    };

    public TimeCounter() {
        passedCount = 0;
    }

    //开始计时
    public void startCount() {
        startCount(true);
    }

    //开始计时
    public void startCount(boolean reset) {
        AndroidConsoleLogPrinter.e(TAG, "startCount: ");
        isDestroyed = false;
        if (reset) {
            passedCount = 0;
        }

        recordTimeHandler.sendEmptyMessage(MSG_TYPE_RECORD_START);
    }

    //中止计时
    public void stopCount() {
        AndroidConsoleLogPrinter.e(TAG, "stopCount: ");
        recordTimeHandler.sendEmptyMessage(MSG_TYPE_RECORD_PAUSE);
    }

    //结束计时
    public void endCount() {
        recordTimeHandler.sendEmptyMessage(MSG_TYPE_RECORD_RESET);
    }

    public void setTimeCallBack(TimeCallBack timeCallBack) {
        this.timeCallBack = timeCallBack;
    }

    //获取当前完成的时间
    public int getpassedCount() {
        return passedCount;
    }

    @Override
    public void onDestroy() {
        recordTimeHandler.removeCallbacksAndMessages(null);
        isDestroyed = true;
    }

    /**
     * 触发的间隔时间
     * @param delayTime
     */
    public void setDelayTime(long delayTime) {
        this.delayTime = delayTime;
    }

    public long getDelayTime() {
        return delayTime;
    }

    public interface TimeCallBack {
        /**
         * @param passedCount       重试了几次
         * @param delayMilliSeconds 重发的时间间隔
         */
        void onTime(int passedCount, long delayMilliSeconds);
    }
}
